# ref: https://devblogs.microsoft.com/scripting/save-a-microsoft-excel-workbook-as-a-pdf-file-by-using-powershell/
# run as administrator priviledge

$path = Get-Location 
$xlFixedFormat = "Microsoft.Office.Interop.Excel.xlFixedFormatType" -as [type]
$excelFiles = Get-ChildItem -Path $path -include *.xls, *.xlsx -recurse
$objExcel = New-Object -ComObject excel.application
$objExcel.visible = $false
foreach($wb in $excelFiles)
{
    # $filepath = Join-Path -Path $path -ChildPath ($wb.BaseName + ".pdf")
    $src_file = Get-Item -Path $wb.FullName
    $dst_filepath = $wb.FullName.Substring(0,($wb.FullName.LastIndexOf('.')))+'.pdf'
    if((Test-Path -Path $dst_filepath) -and ($src_file.LastWriteTime -le (Get-Item -Path $dst_filepath).LastWriteTime))  {
        Write-Output "$($dst_filepath) has been already generated. Skip it."
    } else {
        $workbook = $objExcel.workbooks.open($wb.fullname, 3)
        $workbook.Saved = $true
        "saving $dst_filepath"
        $workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $dst_filepath)
        $objExcel.Workbooks.close()
    }
}
$objExcel.Quit()